package org.techmore.bussiness;

import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.techmore.common.utils.activity.ActivityUtils;
import org.techmore.hakka.LocationShowActivity;
import org.techmore.hakka.R;
import org.techmore.hakka.map.location.MapLocationActitvity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.PopupWindow;

public class MainActivityDataHandler {

	private static MainActivityDataHandler mainActivityDataHandler = new MainActivityDataHandler();

	public static MainActivityDataHandler getInstance() {
		return mainActivityDataHandler;
	}

	private Handler locationHandler = null;
	private LocationManager locationManager = null;
	
	private AlertDialog noLocationAlertDialog = null;

	private Timer timer = new Timer();

	public void getLocation(final Activity activity, final Context context, final PopupWindow mainPopupWindow) {

		locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
		List<String> list = locationManager.getProviders(getLocationCriteria(), false);
		if(list.size()==0){
			list = locationManager.getAllProviders();
		}
		
		initNoLocationAlertDialog(activity,context);
		
		locationHandler = new Handler(Looper.myLooper()) {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				
				try {
					mainPopupWindow.dismiss();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				if(msg.what == 0){
					System.out.println("00000000");
					ActivityUtils.getInstance().
						leftIN_rightOUT_Transition(activity, context, LocationShowActivity.class);
//					noLocationAlertDialog.show();
				}else{
					//-------------切入點--------------
					System.out.println("111111111");
				}
				
			}
		};

		timer.schedule(new LocationTimerTask(list, locationHandler, new Date().getTime()), 0, 1000);
		
	}
	
	
	private void initNoLocationAlertDialog(final Activity activity,final Context context){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(R.string.main_activity_alertdialog_noLocationAlertDialog);
		builder.setNegativeButton(R.string.main_activity_alertdialog_cancel, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				noLocationAlertDialog.dismiss();
				
			}
		});
		builder.setPositiveButton(R.string.main_activity_alertdialog_confirm, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				noLocationAlertDialog.dismiss();
				activity.startActivityForResult(new Intent(context, MapLocationActitvity.class), 0);
			}
		});
		noLocationAlertDialog = builder.create();
	}
	

	private class LocationTimerTask extends TimerTask {
		private List<String> list = null;
		private Location location = null;
		private Handler locationHandler = null;
		private long beginTime = 0;
		public LocationTimerTask(List<String> list,Handler locationHandler,long beginTime) {
			this.list = list;
			this.locationHandler = locationHandler;
			this.beginTime = beginTime;
		}
		long k = 0;
		@Override
		public void run() {
			for (int i = 0; i < list.size(); i++) {
//				System.out.println("1111111111:" + list.get(i));
				location = locationManager.getLastKnownLocation(list.get(i));
				if (null == location && i < list.size()) {
					continue;
				} else {
					break;
				}
			}
			if (null != location) {
//				System.out.println("1111111:=" + location.getLatitude() + ":" + location.getLongitude());
				Message message = new Message();
				message.obj = location;
				locationHandler.sendMessage(message);
			}
			
			k = new Date().getTime();
			if(k - beginTime>3000){
				Message message = new Message();
				message.what = 0;
				locationHandler.sendMessage(message);
				cancel();
			}
		}
	}

	private Criteria getLocationCriteria() {
		Criteria mcriteria = new Criteria();
		mcriteria.setAccuracy(Criteria.ACCURACY_FINE);// 设置为最大精度
		mcriteria.setAltitudeRequired(false);// 不要求海拔信息
		mcriteria.setBearingRequired(false);// 不要求方位信息
		mcriteria.setCostAllowed(true);// 是否允许付费
		mcriteria.setPowerRequirement(Criteria.POWER_LOW);// 对电量要求
//		String strLocationProvider = locationManager.getBestProvider(mcriteria, true);
//		System.out.println(strLocationProvider);
		return mcriteria;
	}

}
